using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using LS.Localization;
using GameFramework.Localization;
using System.IO;

namespace LS.Editor
{
    internal class LocalizationSettingUtil
    {
        public static readonly string DictionaryTemplatePath = "Packages/com.lancerstudio.lsframework/Editor/Assets/CodeTemplate/DictionaryTemplate.xml.txt";
        public static T[] GetAllProjectAssets<T>(string fileName = null, string[] searchPath = null) where T : Object
        {
            return LSPathEditor.FindAssetsFromProject<T>(fileName, searchPath);
        }

        public static LocalizationSetting CreateOrGetLocalizationSetting(string createingPath)
        {
            var localizationSettings = GetAllProjectAssets<LocalizationSetting>();
            if (localizationSettings.Length > 0)
            {
                return localizationSettings[0];
            }
            LocalizationSetting setting = ScriptableObject.CreateInstance<LocalizationSetting>();

            AssetDatabase.CreateAsset(setting, createingPath);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            return setting;
        }

        public static LanguageSetting CreateOrGetLanguageSetting(Language language, string creatingPath)
        {
            LanguageSetting[] dataPath = GetAllProjectAssets<LanguageSetting>();
            LanguageSetting setting = null;
            if (dataPath != null && dataPath.Length > 0)
            {
                setting = dataPath.FirstOrDefault(d => d.Language == language);
                if (setting != null)
                    return setting;
            }

            string directory = Path.GetDirectoryName(creatingPath);
            if (!Directory.Exists(directory))
            {
                string parentFolder = Path.GetDirectoryName(directory);
                string dirName = Path.GetFileName(directory);
                AssetDatabase.CreateFolder(parentFolder, dirName);
            }

            setting = ScriptableObject.CreateInstance<LanguageSetting>();
            setting.Language = language;

            AssetDatabase.CreateAsset(setting, creatingPath);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            return setting;
        }

        public static void DeleteLanguageSetting(LanguageSetting language)
        {
            string path = AssetDatabase.GetAssetPath(language);
            AssetDatabase.DeleteAsset(path);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        public static void CreateDirectory()
        {
            ProjectWindowUtil.CreateScriptAssetFromTemplateFile(DictionaryTemplatePath, "Dictionary.xml");
        }

        public static TextAsset CreateDirectory(string filePath)
        {
            if (!filePath.StartsWith("Assets"))
            {
                filePath = GetSelectedFolderPath() + "/" + filePath;
            }
            File.WriteAllText(filePath, LSCodeTemplate.Instance.DictionaryTemplate.text);
            AssetDatabase.Refresh();
            TextAsset newDictionary = AssetDatabase.LoadAssetAtPath<TextAsset>(filePath);
            return newDictionary;
        }

        public static string GetSelectedFolderPath()
        {
            DefaultAsset selectedFolder = Selection.GetFiltered<DefaultAsset>(SelectionMode.Assets).First();
            if (selectedFolder != null)
            {
                string path = AssetDatabase.GetAssetPath(selectedFolder);
                return path;
            }
            return "Assets";
        }
    }
}
